package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

import model.daos.PlayerDao;
import model.daos.TeamDao;
import model.dtos.Team;

import views.windows.RegisterTeamPanel;

public class RegisterTeamController extends RegisterTeamPanel
{
	private static final long serialVersionUID = 1L;
	TeamDao teamDao;
	PlayerDao playerDao;
	File filePlayers;
	
	public RegisterTeamController()
	{
		super();
		teamDao = new TeamDao();
		playerDao = new PlayerDao();
		
		addActions();
	}
	
	public void cleanFields()
	{
		getCodigoTextField().setText("");
		getNombreTextField().setText("");
		getCiudadTextField().setText("");
		getDateField().setDate(null);
		getEstadioTextField().setText("");
		getJugadoresTextField().setText("");
		filePlayers = null;
	}
	
	public void addActions()
	{
		getRegistrarButton().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				
				if (getCodigoTextField().getText().trim().length() == 0 || getNombreTextField().getText().trim().length() == 0 ||
						getCiudadTextField().getText().trim().length() == 0 || getDateField().getDate() == null ||
								getEstadioTextField().getText().trim().length() == 0 || filePlayers == null)
				{
					showMessage("Datos Incompletos");
				}
				else
				{
					Team team = new Team();
					team.setCode(getCodigoTextField().getText());
					team.setName(getNombreTextField().getText());
					team.setCity(getCiudadTextField().getText());
					team.setFoundedDate(getDateField().getDate());
					team.setStadiumName(getEstadioTextField().getText());
					teamDao.save(team);
					
					try
					{
						playerDao.loadPlayersFromFile(getCodigoTextField().getText(), filePlayers);
					} catch (IOException e)
					{
						e.printStackTrace();
					} catch (ParseException e)
					{
						e.printStackTrace();
					}
					
					showMessage("Equipo registrado con exito");
					cleanFields();
				}
			}
		});
		
		getCancelarButton().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				cleanFields();
			}
		});
		
		getChooserButton().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				JFileChooser chooser = new JFileChooser();
				FileNameExtensionFilter fileNameExtensionFilter = new FileNameExtensionFilter("TXT & CSV", "txt", "csv");
				chooser.setFileFilter(fileNameExtensionFilter);
				chooser.showOpenDialog(RegisterTeamController.this);
				
				if (chooser.getSelectedFile() != null)
				{
					filePlayers = chooser.getSelectedFile();
					getJugadoresTextField().setText(filePlayers.getName());
				}
			}
		});
	}

}
